/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.vs.kindlecoll.Beans;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

/**
 *
 * @author volodymyr.sadovyy
 */
public class BooksCollection extends BookObject {
    private String name;
    private List<Book> books;
    public final static String EMPTY_COLL_MESS = "<Empty Collection>";

    public BooksCollection(String n) {
        name = n;
        this.books = new ArrayList<Book>();
        books.add(new Book(EMPTY_COLL_MESS));
    }
    
    public BooksCollection(String n, List<Book> b) {
        name = n;
        this.books = b;
    }
    
    public void addBook(Book b){
        //remove EMPTY_COLL_MESS from the collection first.
        if (1 == books.size() && books.contains(new Book(EMPTY_COLL_MESS))){
            books.remove(new Book(EMPTY_COLL_MESS));
        }
        this.books.add(b);
    }

    public boolean removeBook(Book b){
        boolean res = this.books.remove(b);
        if (books.isEmpty()){
            books.add(new Book(EMPTY_COLL_MESS));
        }
        return res;
    }
    
    public List<Book> getBooks(){
        return this.books;
    }
    
    @Override
    public String toString(){
        //Cut code after the name
        if (name.indexOf('@') > 0)
            return name.substring(0, name.indexOf('@'));
        else
            return name;
    }
    
    @Override
    public boolean equals(Object o) {
        if (o.getClass().equals(BooksCollection.class)) {
            return (this.getName().equals(((BooksCollection)o).getName()));
        } else {
            return false;
        }
    }

    @Override
    public int hashCode() {
        int hash = 3;
        hash = 43 * hash + (this.name != null ? this.name.hashCode() : 0);
        return hash;
    }

    @Override
    public String getPropertiesString(){
        return "name: " + name + "\n" + 
                "books count: " + books.size() + "\n";
    }

    public List<String> getBookHashes() {
        List<String> hashes = new ArrayList<String>(books.size());
        for (Book b: books){
            hashes.add(b.getKindleHash());
        }
        return hashes;
    }

    public String getName() {
        return name;
    }

    public void setName(String n) {
        name = n;
    }

    
}
